#include "func.h"

/// 比较两个字符串的大小忽略大小写(指明长度)
int aranya_strnicmp(const char* _lhs, const char* _rhs, unsigned int _count)
{
	register signed char __res = 0;
	while(_count && *_lhs && *_rhs)
	{
		if((__res = tolower((unsigned char)*_lhs++) - tolower((unsigned char)*_rhs++)) != 0)
			break;
		--_count;
	}
	return (int)__res;
}

/// 比较两个字符串的大小忽略大小写(未指明长度)
int aranya_stricmp(const char* _lhs, const char* _rhs)
{
	register signed char __res = 0;
	while(*_lhs && *_rhs)
		if((__res = (unsigned char)tolower(*_lhs++) - (unsigned char)tolower(*_rhs++)) != 0)
			break;
	return (int)__res;
}

////////////////////////////////////////////
/// 十六进制字符转成十进制的int
int hexc_to_i(char hex)
{
	if (hex >= '0' && hex <= '9')
		return (hex - '0');
	else if (hex >= 'a' && hex <= 'f')
		return (hex - 'a' + 10);
	else if (hex >= 'A' && hex <= 'F')
		return (hex - 'A' + 10);
	else
		return -1;
}
////////////////////////////////////////////

/// 二维数组分配内存
void **darray_new(int row, int col, int size)
{
	void **arr = NULL;

	arr = (void **)malloc(sizeof(void *) * row + size * row * col);
	if(arr)
	{
		void **head = NULL;
		head = arr + sizeof(void **) * row;
		memset(arr, 0, sizeof(void **) * row + size * row * col);
		while(row--)
		{
			arr[row] = head + size * row * col;
		}
	}
	return arr;
}

/// 二维数组内存释放
void darray_del(void **arr)
{
	if(arr != NULL)
		free(arr);
}
